﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Evan.Common
{
    /// <summary>
    /// 系统公用类 
    /// </summary>
    public static class SysComm
    {
        /// <summary>
        /// 获取站点配置 , 使用WebConfig中的SiteConfigId拿到Versions中的对应数据
        /// <para>增加 WebPath 如果没有在后台配置WebPath则默认为 / 站点根目录</para>
        /// <para>增加 WebAdminPath 后台地址 , 默认是WebPath加上后台配置的管理目录</para>
        /// </summary>
        /// <returns></returns>
        public static dynamic GetSiteConfig()
        {
            dynamic SiteConfig = new DataBase.DbOperat("Versions").GetModel(Common.CommFun.GetAppSetting("SiteConfigId").ToInt32());
            SiteConfig.Set("WebPath", string.IsNullOrEmpty(SiteConfig.WebPath as string) ? "/" : SiteConfig.WebPath);
            SiteConfig.Set("WebAdminPath", SiteConfig.WebPath + SiteConfig.WebManagerPath + "/");
            return SiteConfig;
        }
        /// <summary>
        /// 获取站点配置 , 指定的SiteConfigId拿到Versions中的对应数据
        /// <para>增加 WebPath 如果没有在后台配置WebPath则默认为 / 站点根目录</para>
        /// <para>增加 WebAdminPath 后台地址 , 默认是WebPath加上后台配置的管理目录</para>
        /// </summary>
        /// <param name="Id">Version表中的Id</param>
        /// <returns></returns>
        public static dynamic GetSiteConfig(int Id)
        {
            dynamic SiteConfig = new DataBase.DbOperat("Versions").GetModel(Id);
            SiteConfig.Set("WebPath", string.IsNullOrEmpty(SiteConfig.WebPath as string) ? "/" : SiteConfig.WebPath);
            SiteConfig.Set("WebAdminPath", SiteConfig.WebPath + SiteConfig.WebManagerPath + "/");
            return SiteConfig;
        }

        /// <summary>
        /// 获取系统配置 , 在 system.config
        /// <para>优先从缓存中获取 systemConfig</para>
        /// </summary>
        /// <returns></returns>
        public static dynamic GetSystemConfig()
        {
            object config = Common.CacheHelp.GetValue("systemConfig");
            dynamic SystemConfig = new Evan.Model.DynamicModel();
            if (config == null)
            {
                //如果缓存里面没有  则重新查找 查找完成后写入缓存
                string ConfigPath = "~/App_Data/SysConfig/system.config";
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml(Evan.Common.DirFile.ReadFile(ConfigPath));
                XmlNodeList xn = xmldoc.SelectNodes(string.Format("/root/category/item"));
                foreach (XmlElement item in xn)
                {
                    SystemConfig.Set(item.Attributes["name"].Value, item.InnerText);
                }
                Dictionary<string, object> configInfo = Evan.Model.ConvertModel.ConvertToDictionary(SystemConfig);
                Common.CacheHelp.Insert_NoAbsolute_Rank("systemConfig", configInfo, Evan.Common.CacheHelp.CacheTimeOut, Evan.Common.CacheHelp.GetRank(6));
            }
            else
            {
                SystemConfig = Evan.Model.ConvertModel.ConvertToDynamic((Dictionary<string, object>)Evan.Common.CacheHelp.GetValue("systemConfig"));
            }
            return SystemConfig;
        }

        /// <summary>
        /// 获取栏目排序规则
        /// </summary>
        /// <returns></returns>
        public static  Dictionary<string, string> GetChannelOrderBy()
        {
            Dictionary<string, string> SortBy = new Dictionary<string, string>();
            SortBy.Add("0", "添加时间降序 (时间越新越靠前)");
            SortBy.Add("1", "添加时间升序 (时间越旧越靠前)");
            SortBy.Add("2", "排序编号降序 (编号越大越靠前)");
            SortBy.Add("3", "排序编号升序 (编号越小越靠前)");
            return SortBy;
        }

        /// <summary>
        /// 获取当前系统是否模块出生
        /// </summary>
        /// <returns></returns>
        public static bool IsModuleSystem()
        {
            return Evan.Common.CommFun.GetAppSetting("IsModuleSystem") == "1";
        }

        /// <summary>
        /// 获取当前系统是否开发模式
        /// </summary>
        /// <returns></returns>
        public static bool IsDevModel()
        {
            return Evan.Common.CommFun.GetAppSetting("DevMode") == "1";
        }

        /// <summary>
        /// 获取当前站点的安全等级 , webconfig中默认是没有low的
        /// <para>low -1</para>
        /// <para>normal 0</para>
        /// <para>height 1</para>
        /// <para>best 2</para>
        /// </summary>
        /// <returns></returns>
        public static int SecurityLevel()
        {
            string sl = Evan.Common.CommFun.GetAppSetting("SecurityLevel").ToLower().Trim();
            int slv = 0;
            switch (sl)
            {
                case "low":
                    slv = -1;
                    break;
                case "normal":
                    slv = 0;
                    break;
                case "height":
                    slv = 1;
                    break;
                case "best":
                    slv = 2;
                    break;
                default:
                    slv = 0;
                    break;
            }
            return slv;
        }
    }
}
